﻿using ExpressesClass.BestApi.Utils;
using ExpressesClass.Method;
using ExpressesClass.Model;
using OFFICE_Helper.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace OFFICE_Helper.Forms
{
    public partial class MainForm : Form
    {

        #region 变量
        private List<Sender> list_man;
        #endregion

        #region 方法
        public MainForm()
        {
            InitializeComponent();
            list_man = new List<Sender>();
        }
        public void InitCtrl()
        {
            dtp_begin.Format = DateTimePickerFormat.Custom;
            dtp_begin.CustomFormat = "yyyy-MM-dd";
            dtp_end.Format = DateTimePickerFormat.Custom;
            dtp_end.CustomFormat = "yyyy-MM-dd";
            dtp_begin.Value = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd 00:00:00"));
            dtp_end.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

            InitMans();
        }
        public void InitMans()
        {
            dgv_man.DataSource = new List<Sender>();
            dgv_man.Columns["Address"].Width = 300;
            dgv_man.Columns["Name"].HeaderText = "收件人姓名";
            dgv_man.Columns["Phone"].HeaderText = "收件人电话";
            dgv_man.Columns["Address"].HeaderText = "收件人地址";
            dgv_man.Columns["Postcode"].HeaderText = "收件邮编";
            dgv_man.Columns["Province"].HeaderText = "收件省";
            dgv_man.Columns["City"].HeaderText = "收件市";
            dgv_man.Columns["County"].HeaderText = "收件区县";
        }
        public void InitComboBox()
        {
            try
            {
                string companys = ConfigurationManager.AppSettings["companys"].ToString();
                cb_companys.DataSource = JsonUtil.parse<List<Company>>(companys);
                cb_companys.DisplayMember = "ShowName";

                string kdcompanys = ConfigurationManager.AppSettings["kdcompanys"].ToString();
                cb_kdCompany.DataSource = JsonUtil.parse<List<string>>(kdcompanys);

                tb_senderName.Text = ConfigurationManager.AppSettings["off-send-name"].ToString();
                tb_senderPhone.Text = ConfigurationManager.AppSettings["off-send-phone"].ToString();
                tb_senderAddress.Text = ConfigurationManager.AppSettings["off-send-address"].ToString();
                tb_senderPostcode.Text = ConfigurationManager.AppSettings["off-send-postcode"].ToString();
                tb_senderProvince.Text = ConfigurationManager.AppSettings["off-send-province"].ToString();
                tb_senderCity.Text = ConfigurationManager.AppSettings["off-send-city"].ToString();
                tb_senderCounty.Text = ConfigurationManager.AppSettings["off-send-county"].ToString();
                tb_remark.Text = ConfigurationManager.AppSettings["off-send-remark"].ToString();
                tb_goodsName.Text = ConfigurationManager.AppSettings["off-send-itemName"].ToString();
            }
            catch
            {
                MessageBox.Show("App.config文件解析出错,请检查.");
            }
        }
        public void Init()
        {
            string company = ConfigurationManager.AppSettings["curCompany"].ToString();
            list_man = DbUtil.GetManMsg(DbUtil.Kddh_Query(dtp_begin.Value, dtp_end.Value, Kddh.ScenariosType.OFFICE, company));
            ShowView();
        }
        public void ShowView()
        {
            if (list_man == null)
            {
                return;
            }
            SortableBindingList<Sender> ds = new SortableBindingList<Sender>(list_man);
            dgv_man.DataSource = ds;
        }
        public void SetCellsColor(DataGridView dgv, string text)
        {
            for (int r = 0; r < dgv.Rows.Count; r++)
            {
                for (int c = 0; c < dgv.Columns.Count; c++)
                {
                    if (dgv.Rows[r].Cells[c].Value != null && dgv.Rows[r].Cells[c].Value.ToString().Contains(text))
                    {
                        dgv.Rows[r].Cells[c].Style.BackColor = Color.YellowGreen;
                    }
                }
            }
        }
        public bool Check()
        {
            tb_senderName.Text = tb_senderName.Text.Trim().Replace(" ", "");
            tb_senderPhone.Text = tb_senderPhone.Text.Trim().Replace(" ", "");
            tb_senderAddress.Text = tb_senderAddress.Text.Trim().Replace(" ", "");
            tb_senderProvince.Text = tb_senderProvince.Text.Trim().Replace(" ", "");
            tb_senderCity.Text = tb_senderCity.Text.Trim().Replace(" ", "");
            tb_senderCounty.Text = tb_senderCounty.Text.Trim().Replace(" ", "");
            tb_receiveName.Text = tb_receiveName.Text.Trim().Replace(" ", "");
            tb_receivePhone.Text = tb_receivePhone.Text.Trim().Replace(" ", "");
            tb_receiveAddress.Text = tb_receiveAddress.Text.Trim().Replace(" ", "");
            tb_receiveProvince.Text = tb_receiveProvince.Text.Trim().Replace(" ", "");
            tb_receiveCity.Text = tb_receiveCity.Text.Trim().Replace(" ", "");
            tb_receiveCounty.Text = tb_receiveCounty.Text.Trim().Replace(" ", "");

            if (tb_senderName.Text == "" || tb_senderPhone.Text == "" || tb_senderAddress.Text == "" || tb_senderProvince.Text == "" || tb_senderCity.Text == "" || tb_senderCounty.Text == "" ||
                tb_receiveName.Text == "" || tb_receivePhone.Text == "" || tb_receiveAddress.Text == "" || tb_receiveProvince.Text == "" || tb_receiveCity.Text == "" || tb_receiveCounty.Text == "")
            {
                MessageBox.Show("必填项不能为空,请填写.", "提示");
                return false;
            }
            return true;
        }
        public Kddh Package()
        {
            Company selectCompany = (Company)cb_companys.SelectedItem;

            Kddh k = new Kddh();
            k.sendMan = tb_senderName.Text;
            k.sendManPhone = tb_senderPhone.Text;
            k.sendManAddress = tb_senderAddress.Text;
            k.sendPostcode = tb_senderPostcode.Text;
            k.sendProvince = tb_senderProvince.Text;
            k.sendCity = tb_senderCity.Text;
            k.sendCounty = tb_senderCounty.Text;
            k.sendCompany = selectCompany.Name;
            k.receiveMan = tb_receiveName.Text;
            k.receiveManPhone = tb_receivePhone.Text;
            k.receiveManAddress = tb_receiveAddress.Text;
            k.receivePostcode = tb_receivePostcode.Text;
            k.receiveProvince = tb_receiveProvince.Text;
            k.receiveCity = tb_receiveCity.Text;
            k.receiveCounty = tb_receiveCounty.Text;
            k.receiveCompany = "";
            k.itemCount = nud_count.Value.ToString();
            k.itemName = tb_goodsName.Text;
            k.itemWeight = nud_weight.Value.ToString();
            k.remark = tb_remark.Text.Length > 90 ? tb_remark.Text.Substring(0, 90) + "..." : tb_remark.Text;
            k.bm = "";
            k.wzh = "";
            k.insuranceValue = nud_insuranceValue.Value.ToString();
            k.kdCompanys = cb_kdCompany.Text;
            k.shipName = cb_sendtype.Text;
            k.shipVal = nud_money.Value.ToString();
            k.payMethod = cb_payMethod.Text;
            k.isReturnNo = cb_returnNo.Text;
            k = TypeControl.Format(k);
            return k;
        }
        #endregion

        #region 事件
        private void OfficeForm_Load(object sender, EventArgs e)
        {
            InitCtrl();
            InitComboBox();
            Init();
        }
        private void cb_companys_SelectedIndexChanged(object sender, EventArgs e)
        {
            Company selectCompany = (Company)cb_companys.SelectedItem;
            tb_senderAddress.Text = selectCompany.Address;
            tb_senderPostcode.Text = selectCompany.Postcode;
            tb_senderProvince.Text = selectCompany.Province;
            tb_senderCity.Text = selectCompany.City;
            tb_senderCounty.Text = selectCompany.County;
        }
        private void cb_sendtype_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = cb_sendtype.SelectedItem.ToString();
            if (text == "货到付款" || text == "代收货款")
            {
                lb_money.Visible = true;
                nud_money.Visible = true;
            }
            else
            {
                lb_money.Visible = false;
                nud_money.Visible = false;
            }
        }
        private void btn_search_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void dgv_man_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgv_man.SelectedRows.Count == 0)
            {
                return;
            }
            Sender k = (Sender)dgv_man.SelectedRows[0].DataBoundItem;

            tb_receiveName.TextChanged -= tb_receiveName_TextChanged;
            tb_receivePhone.TextChanged -= tb_receivePhone_TextChanged;

            tb_receiveName.Text = k.Name;
            tb_receivePhone.Text = k.Phone;
            tb_receiveAddress.Text = k.Address;
            tb_receivePostcode.Text = k.Postcode;
            tb_receiveProvince.Text = k.Province;
            tb_receiveCity.Text = k.City;
            tb_receiveCounty.Text = k.County;

            tb_receiveName.TextChanged += tb_receiveName_TextChanged;
            tb_receivePhone.TextChanged += tb_receivePhone_TextChanged;
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                Kddh k = Package();
                ExpressUtil.GetMailNo(k);
            }
        }
        private void btn_bro_Click(object sender, EventArgs e)
        {
            KddhForm f = new KddhForm(this, Kddh.ScenariosType.OFFICE);
            f.Show(this);
        }
        private void btn_batch_Click(object sender, EventArgs e)
        {
            BatchForm f = new BatchForm(this);
            f.Show(this);
        }
        private void btn_useNum_Click(object sender, EventArgs e)
        {
            ExpressUtil.GetUseNum(cb_kdCompany.Text);
        }
        private void tb_receiveName_TextChanged(object sender, EventArgs e)
        {
            string text = tb_receiveName.Text;
            SortableBindingList<Sender> ds = new SortableBindingList<Sender>(list_man.Where(k => k.Name.Contains(text)).ToList());
            dgv_man.DataSource = ds;
            if (text.Length > 0)
            {
                SetCellsColor(dgv_man, text);
            }
        }
        private void tb_receivePhone_TextChanged(object sender, EventArgs e)
        {
            string text = tb_receivePhone.Text;
            SortableBindingList<Sender> ds = new SortableBindingList<Sender>(list_man.Where(k => k.Phone.Contains(text)).ToList());
            dgv_man.DataSource = ds;
            if (text.Length > 0)
            {
                SetCellsColor(dgv_man, text);
            }
        }
        private void tb_search_man_TextChanged(object sender, EventArgs e)
        {
            string text = tb_search_man.Text;
            SortableBindingList<Sender> ds = new SortableBindingList<Sender>(list_man.Where(
                k => k.Name.Contains(text)
                || k.Phone.Contains(text)
                || k.Address.Contains(text)
                || k.Postcode.Contains(text)
                || k.Province.Contains(text)
                || k.City.Contains(text)
                || k.County.Contains(text)
                ).ToList());
            dgv_man.DataSource = ds;
            if (text.Length > 0)
            {
                SetCellsColor(dgv_man, text);
            }
        }
        private void cb_kdCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sendtypes = ConfigurationManager.AppSettings["sendTypes"].ToString();
                string payMethod = ConfigurationManager.AppSettings["payMethod"].ToString();
                string returnNo = ConfigurationManager.AppSettings["isReturnNo"].ToString();
                if (cb_kdCompany.Text == "百世快递")
                {
                    sendtypes = ConfigurationManager.AppSettings["bestSendTypes"].ToString();
                }
                else if (cb_kdCompany.Text == "顺丰快递")
                {
                    sendtypes = ConfigurationManager.AppSettings["sfSendTypes"].ToString();
                    payMethod = ConfigurationManager.AppSettings["sfPayMethod"].ToString();
                    returnNo = ConfigurationManager.AppSettings["sfIsReturnNo"].ToString();
                }
                else if (cb_kdCompany.Text == "德邦快递")
                {
                    payMethod = ConfigurationManager.AppSettings["depponPayMethod"].ToString();
                }
                cb_sendtype.DataSource = JsonUtil.parse<List<string>>(sendtypes);
                cb_payMethod.DataSource = JsonUtil.parse<List<string>>(payMethod);
                cb_returnNo.DataSource = JsonUtil.parse<List<string>>(returnNo);
            }
            catch
            {
                MessageBox.Show("App.config解析出错,请检查.");
            }
        }
        private void OfficeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConfigUtil.SetValue("off-send-name", tb_senderName.Text);
            ConfigUtil.SetValue("off-send-phone", tb_senderPhone.Text);
            ConfigUtil.SetValue("off-send-address", tb_senderAddress.Text);
            ConfigUtil.SetValue("off-send-postcode", tb_senderPostcode.Text);
            ConfigUtil.SetValue("off-send-province", tb_senderProvince.Text);
            ConfigUtil.SetValue("off-send-city", tb_senderCity.Text);
            ConfigUtil.SetValue("off-send-county", tb_senderCounty.Text);
            ConfigUtil.SetValue("off-send-remark", tb_remark.Text);
            ConfigUtil.SetValue("off-send-itemName", tb_goodsName.Text);
        }
        #endregion
    }
}
